package com.itheima.service.backend.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.bo.SetmealListBO;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.backend.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.backend.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    SetmealService  setmealService;
    @Autowired
    private Configuration configuration;

    /**
     * 添加套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealMapper.add(setmeal);
        for (Integer checkgroupId : checkgroupIds){
            setmealMapper.addSetMealAndCheckGroup(setmeal.getId(), checkgroupId);
        }
    }

    /**
     * 编辑套餐
     * @param setmeal
     * @param checkGroupIds
     */
    @Override
    public void edit(Setmeal setmeal, Integer[] checkGroupIds) {
        setmealMapper.edit(setmeal);
        setmealMapper.deleteSetMealAndCheckGroup(setmeal.getId());
        for (Integer checkGroupId : checkGroupIds) {
            setmealMapper.addSetMealAndCheckGroup(setmeal.getId(), checkGroupId);
        }
    }

    /**
     * 删除套餐
     * @param id
     */
    @Override
    public void delete(Integer id) {
        setmealMapper.deleteSetMealAndCheckGroup(id);
        setmealMapper.delete(id);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Object> page = setmealMapper.findPage(queryPageBean);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        return setmealMapper.findById(id);
    }

    /**
     * 创建套餐页面
     */
    @Override
    public void createSetmealPage() throws IOException, TemplateException {
        // 查询所有套餐
        QueryPageBean queryPageBean = new QueryPageBean();
        queryPageBean.setCurrentPage(1);
        queryPageBean.setPageSize(10);
        queryPageBean.setQueryString("");
        PageResult pageResult = setmealService.findPage(queryPageBean);
        // 遍历套餐,传给setmealListBOS集合对象
        List<SetmealListBO> SetmealListBOS = new ArrayList<>();
        for (Object o : pageResult.getRows()) {
            Setmeal setmeal = (Setmeal) o;
            SetmealListBO setmealListBO = SetmealListBO.builder()
                    .id(setmeal.getId())
                    .img(setmeal.getImg())
                    .name(setmeal.getName())
                    .sex(setmeal.getSex())
                    .price(setmeal.getPrice())
                    .remark(setmeal.getRemark())
                    .age(setmeal.getAge())
                    .build();
            SetmealListBOS.add(setmealListBO);
        }
        // 将列表放入 Map 中，作为模板的数据模型
        Map<String, Object> dataModel = new HashMap<>();
        dataModel.put("setmealList", SetmealListBOS);
        // 创建套餐页面
        Template template = configuration.getTemplate("mobile_setmeal.ftl");
        template.process(dataModel, new FileWriter(new File("D:\\code\\bxg-health-parent\\bxg-health-mobile\\src\\main\\resources\\mobile\\pages2\\mobile_setmeal.html")));
    }

    /**
     * 生成套餐详情静态页面
     */
    @Override
    public void createSetmealDetailPage() throws IOException, TemplateException{
        //查询所有套餐
        List<Setmeal> setmeals = setmealMapper.findSetmeals();
        //查询所有套餐关联的检查组和检查项
        for (Setmeal setmeal : setmeals) {
            Setmeal setmeal1 = setmealMapper.findSetmealById(setmeal.getId());
            for (CheckGroup checkGroup : setmeal1.getCheckGroups()) {
                checkGroup.setCheckItems(setmealMapper.findCheckItemByCheckGroupId(checkGroup.getId()));
            }
            setmeal.setCheckGroups(setmeal1.getCheckGroups());
        }
        //  遍历套餐,创建套餐详情页面
        for (Setmeal setmeal : setmeals) {
            // 将列表放入 Map 中，作为模板的数据模型
            Map<String, Object> dataModel = new HashMap<>();
            dataModel.put("setmeal", setmeal);
            // 创建套餐页面
            String path = "D:\\//code\\bxg-health-parent\\bxg-health-mobile\\src\\main\\resources\\mobile\\pages2\\mobile_setmeal_detail_"+setmeal.getId()+".html";
            Template template = configuration.getTemplate("mobile_setmeal_detail.ftl");
            template.process(dataModel, new FileWriter(new File(path)));
        }
    }
}
